#!/usr/bin/env python
# -*- coding:utf-8 -*-
#

def get_sys_file(sys_file):
    with open(sys_file,'r') as f:
        sys_info_list = []
        tmp = [ line.replace("{","") for line in f.readlines() if line.strip() ]
        
        tmp = ''.join(tmp).split('}')
        #print tmp
        for x in tmp:
            item = x.replace("'","").split('\n')
            #print item
            #item_list.append(item)
            for xx in item:
                it = xx.split(',')
                if len(it) > 1:
                    print it
                    for xxx in it:
                         sys_info = {}
                        i = xxx.split(':')
                        print i
                        sys_info[str(i[0]).strip()] = i[1].strip()
                else:
                    continue
                sys_info_list.append(sys_info)
        #print sys_info_list
    #print sys_info_list
    #return sys_info_list
get_sys_file('/tmp/sys_file.tmp')
def export_csv():
    sys_file = '/tmp/sys_file.tmp'
    csv_file = '/tmp/test.csv'
    
    #fieldnames = ['HOST','IP','CPU_NUM','MEM_TOTAL','cpu_5_12_max_percent','mem_5_12_max_percent','cpu_12_5_max_percent','mem_12_5_max_percent']
    fieldnames = ['主机名','IP','CPU核数','内存大小','白天CPU峰值','白天内存峰值','夜晚CPU峰值','夜晚内存峰值']
    rows = get_sys_file(sys_file)
    
    csv_f = open(csv_file,"wb+")
    csv_f.write(','.join(fieldnames))
    csv_f.write('\t\n')
    for x in rows:
        print x
        #xx = [x['HOST'],x['IP'],x['CPU_NUM'],x['MEM_TOTAL'],x['cpu_5_12_max_percent'],x['mem_5_12_max_percent'],x['cpu_12_5_max_percent'],x['mem_12_5_max_percent']]
        xx = [x['HOST'],x['IP'],x['CPU_NUM']+'核',str(mem_total)+'GB',x['cpu_5_12_max_percent']+'%',x['mem_5_12_max_percent']+'%',x['cpu_12_5_max_percent']+'%',x['mem_12_5_max_percent']+'%']
        csv_f.write(','.join(xx))
        csv_f.write('\t\n')
    csv_f.close()

#export_csv()